#include "PlanetControls.hh"
#include "Bolygo.hh"

using namespace Gui;

PlanetControls::PlanetControls(QWidget *parent):
    QWidget(parent)
{
	ui.setupUi(this);
	QComboBox* buildCbx = findChild< QComboBox* >("buildCbx");
    buildCbx->addItem("Lakoepulet",     "dwelling");
    buildCbx->addItem("Gyar",           "factory");
    buildCbx->addItem("Farm",           "farm");
    buildCbx->addItem("Reaktor",        "reactor");
    buildCbx->addItem("Csatahajo",      "battleship");
    buildCbx->addItem("Vadasz",         "fighter");
    buildCbx->addItem("Csapatszallíta", "transportship");
    buildCbx->addItem("Kolonizalo",     "colonyship");
    buildCbx->addItem("Tank",           "tank");
}

PlanetControls::~PlanetControls() {}

void PlanetControls::clear() {
    planet = 0;
    QLineEdit* ownerLin    = findChild< QLineEdit* >("ownerLin");
    QLineEdit* sizeLin     = findChild< QLineEdit* >("sizeLin");
    QLineEdit* popLin      = findChild< QLineEdit* >("popLin");
    QLineEdit* foodLin     = findChild< QLineEdit* >("foodLin");
    QLineEdit* dwellingLin = findChild< QLineEdit* >("dwellingLin");
    QLineEdit* factoryLin  = findChild< QLineEdit* >("factoryLin");
    QLineEdit* farmLin     = findChild< QLineEdit* >("farmLin");
    QLineEdit* reactorLin  = findChild< QLineEdit* >("reactorLin");
    QLineEdit* fighterLin  = findChild< QLineEdit* >("fighterLin");
    QLineEdit* tankLin     = findChild< QLineEdit* >("tankLin");
    ownerLin->setText("-");
    sizeLin->setText("-");
    popLin->setText("-");
    foodLin->setText("-");
    dwellingLin->setText("-");
    factoryLin->setText("-");
    farmLin->setText("-");
    reactorLin->setText("-");
    fighterLin->setText("-");
    tankLin->setText("-");
}

void PlanetControls::emit_build() {
    qDebug("slot! PlanetControls::build_clicked");
    QComboBox* buildCbx = findChild< QComboBox* >("buildCbx");
    QLineEdit* amountLin = findChild< QLineEdit* >("amountLin");
    QString what = buildCbx->itemData(buildCbx->currentIndex()).toString();
    bool convOk;
    QString amountStr = amountLin->text();
    u8 amount = amountStr.toULongLong(&convOk);

    if (convOk && planet) {
        qDebug("signal! PlanetControls::build");
        emit build(planet, what, amount);
    }
}

void PlanetControls::update_planet(Bolygo* planet) {
    qDebug("slot! PlanetControls::update_planet");
    this->planet = planet;
    QLineEdit* ownerLin    = findChild< QLineEdit* >("ownerLin");
    QLineEdit* sizeLin     = findChild< QLineEdit* >("sizeLin");
    QLineEdit* popLin      = findChild< QLineEdit* >("popLin");
    QLineEdit* foodLin     = findChild< QLineEdit* >("foodLin");
    QLineEdit* dwellingLin = findChild< QLineEdit* >("dwellingLin");
    QLineEdit* factoryLin  = findChild< QLineEdit* >("factoryLin");
    QLineEdit* farmLin     = findChild< QLineEdit* >("farmLin");
    QLineEdit* reactorLin  = findChild< QLineEdit* >("reactorLin");
    QLineEdit* fighterLin  = findChild< QLineEdit* >("fighterLin");
    QLineEdit* tankLin     = findChild< QLineEdit* >("tankLin");
    ownerLin->setText(planet->planetinfo.owner);
    sizeLin->setText(QString::number(planet->planetinfo.size));
    popLin->setText(QString::number(planet->planetinfo.population));
    foodLin->setText(QString::number(planet->planetinfo.food));
    dwellingLin->setText(QString::number(planet->planetinfo.dwelling));
    factoryLin->setText(QString::number(planet->planetinfo.factory));
    farmLin->setText(QString::number(planet->planetinfo.farm));
    reactorLin->setText(QString::number(planet->planetinfo.reactor));
    fighterLin->setText(QString::number(planet->planetinfo.fighter));
    tankLin->setText(QString::number(planet->planetinfo.tank));
}
